#include "Game.h"
#include "DXUtil.h"
#include <d3dx9.h>
#include <d3d9.h>
#include <iostream>
#include "ModelPrimitive.h"
#include "AnimChangeOverTime.h"


void Game::Initialize(LPDIRECT3DDEVICE9 d3ddev, LPDIRECT3D9 myDirect3D)
{
	mTotalTime = 0.0f;
	D3DXMatrixIdentity(&mIdentity);
	IniScene(d3ddev);
	LoadContent(d3ddev);
}

void Game::Update(LPDIRECT3DDEVICE9 d3ddev, double pElapsedTime) 
{
	mTotalTime += pElapsedTime;	
	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->Update(d3ddev,pElapsedTime, mTotalTime);

		if(mModels[i]->GetName().compare("Airplane") == 0){
		//	mCamera->SetLookAt(*(mModels[i]->GetPos()));
	}
		
		
		if(mModels[i]->GetName().compare("Airplane") == 0){
			if(mModels[i]->GetPos()->y > 50){
				mModels[i]->GetPos()->y = -50.0f;
				mModels[i]->GetPos()->x = -50.0f;
			}
		}
	}

	for(int i = 0; i < mLights.size(); i++ )
	{
		if(mLights[i]->GetOn()){	
		    d3ddev->LightEnable(mLights[i]->GetLightNumber(), true);    // turn on light #0   
		}
		else if(mLights[i]->GetLightNumber() < 0 ){
			d3ddev->LightEnable(mLights[i]->GetLightNumber(),false);
		}
	}
}

D3DVECTOR* Game::DetermineEllipseTangent(D3DVECTOR* circle,D3DVECTOR* point )
{
     // This is just an implementation of the equation in the tutorial. All we need is a vector
     // giving the direction of the tangent, hence we only pay attention to the final terms.
 
     // The variable t is simply a parameter to the equation. We are only paying attention to
     // the final terms, and since they give the slope (i.e., the direction of the tangent), we
     // let t = 1.
 
   //  D3DVECTOR tangent = new D3DVECTOR();
    // tangent.x = point->x - (Ellipse->z* point->y)/Ellipse->z;
     //tangent.y = point->y + (Ellipse->z*point->x)/Ellipse->z;
	return NULL;
   //  return &tangent;     
}

void Game::IniScene(LPDIRECT3DDEVICE9 d3ddev){

	ModelPrimitive* mSphere;
	ModelPrimitive* mSphere2;
	ModelPrimitive* mPlane;

	Model* mAirPlane;

	mSphere = new ModelPrimitive();
	
	mSphere->GetPos()->x = -40.0f;
	mSphere->GetPos()->y = -40.0f;
	mSphere->GetPos()->z = 3;

	mSphere->GetScale()->x = 4.0f;
	mSphere->GetScale()->y = 4.0f;
	mSphere->GetScale()->z = 4.0f;

	mSphere->GetMaterial()->Ambient.r = 1.0f;
	mSphere->GetMaterial()->Ambient.g = 0.0f;
	mSphere->GetMaterial()->Ambient.b = 0.0f;
	mSphere->GetMaterial()->Ambient.a = 1.0f;

	mSphere->GetMaterial()->Diffuse.r = 1.0f;
	mSphere->GetMaterial()->Diffuse.g = 0.0f;
	mSphere->GetMaterial()->Diffuse.b = 0.0f;
	mSphere->GetMaterial()->Diffuse.a = 1.0f;

	mSphere->SetTeapot(d3ddev,NULL);
	
	mModels.push_back(mSphere);

	//Create the second smaller sphere
	mSphere2 = new ModelPrimitive();
	mSphere2->GetPos()->x = 1.25f;
	mSphere2->GetPos()->y = -1.5f;
	mSphere2->GetPos()->z = -1.0f;

	mSphere2->GetScale()->x = 1.0f;
	mSphere2->GetScale()->y = 1.0f;
	mSphere2->GetScale()->z = 1.0f;

	mSphere2->GetMaterial()->Ambient.r = 0.0f;
	mSphere2->GetMaterial()->Ambient.g = 0.65f;
	mSphere2->GetMaterial()->Ambient.b = 0.0f;
	mSphere2->GetMaterial()->Ambient.a = 0.5f;

	mSphere2->GetMaterial()->Diffuse.r = .25f;
	mSphere2->GetMaterial()->Diffuse.g = 1.0f;
	mSphere2->GetMaterial()->Diffuse.b = 0.25f;
	mSphere2->GetMaterial()->Diffuse.a = 1.0f;

	mSphere2->SetSphere(d3ddev,1,30,30,NULL);

	mModels.push_back(mSphere2);

	//Create the Plane 2.0f,-4.0f, 1.0f
	mPlane = new ModelPrimitive();
	mPlane->GetPos()->x = 2.0f;
	mPlane->GetPos()->y = -4.f;
	mPlane->GetPos()->z = 1.0f;

	mPlane->GetScale()->x = 1.0f;
	mPlane->GetScale()->y = 1.0f;
	mPlane->GetScale()->z = 1.0f;

	mPlane->GetMaterial()->Ambient.r = 0.0f;
	mPlane->GetMaterial()->Ambient.g = 0.0f;
	mPlane->GetMaterial()->Ambient.b = 1.0f;
	mPlane->GetMaterial()->Ambient.a = 1.0f;

	mPlane->GetMaterial()->Diffuse.r = 0.0f;
	mPlane->GetMaterial()->Diffuse.g = 0.0f;
	mPlane->GetMaterial()->Diffuse.b = 1.0f;
	mPlane->GetMaterial()->Diffuse.a = 1.0f;
	
	mPlane->SetBox(d3ddev,1.0f,1.0f,1.0f, NULL);

	mModels.push_back(mPlane);

	mAirPlane = new Model("Airplane","airplane 2.x");

	mAirPlane->GetScale()->x = 3.5f;
	mAirPlane->GetScale()->y = 3.5f;
	mAirPlane->GetScale()->z = 3.5f;

	mAirPlane->GetPos()->x = 0.0f;
	mAirPlane->GetPos()->y = 0.0f;
	mAirPlane->GetPos()->z = 0.0f;



	/*
	AnimChangeOverTime* tX = new AnimChangeOverTime(&(mAirPlane->GetPos()->x),5.0f);
	mAirPlane->AddAnimation(tX);
	tX->SetEnabled(true);
	tX = new AnimChangeOverTime(&(mAirPlane->GetPos()->y),5.0f);
	mAirPlane->AddAnimation(tX);
	tX->SetEnabled(true);
	tX = new AnimChangeOverTime(&(mAirPlane->GetRot()->y),2.0f);
	mAirPlane->AddAnimation(tX);
	tX->SetEnabled(true);
*/

	mAirPlane->GetRot()->x = 0.0f;
	mAirPlane->GetRot()->y = 0.0f;
	mAirPlane->GetRot()->z = 0.0f;

	//mAirPlane->SetBox(d3ddev,1.0f,1.0f,1.0f, NULL);
/*
	if(input.compare("") != 0){
	ifstream fp_in;  // declarations of streams fp_in and fp_out
	fp_in.open(input.c_str(), ios::in);    // open the streams
	fp_in >> time;
	if(fp_in.eof()){
		time=time;
	}
	D3DVECTOR pos;
	fp_in >> pos.x;
	fp_in >> pos.y;
	fp_in >> pos.z;
	
	D3DVECTOR angle;
	fp_in >> angle.x;
	fp_in >> angle.y;
	fp_in >> angle.z;
	float amount;
	fp_in >> amount;
	
	mAirPlane->AddKeyFrame(new KeyFrame(pos.x,pos.y,pos.z,&angle,DEGTORAD(amount),time));		

	fp_in.close();   // close the streams

	}
	else{
	*/
	D3DVECTOR angle;
	angle.x = 1.0f;
	angle.y = 1.0f;
	angle.z = -1.0f;
	
	mAirPlane->AddKeyFrame(new KeyFrame(4,0,0,&angle,DEGTORAD(30),1000));	

	angle.x = 1.0f;
	angle.y = 1.0f;
	angle.z = -1.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(0,0,0,&angle,DEGTORAD(90),9000));
	
	angle.x = 1.0f;
	angle.y = 1.0f;
	angle.z = -1.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(12,12,12,&angle,DEGTORAD(180),15000));

	angle.x = 1.0f;
	angle.y = 1.0f;
	angle.z = -1.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(12,18,18,&angle,DEGTORAD(270),4000));
	
	angle.x = 0.0f;
	angle.y = 1.0f;
	angle.z = 0.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(18,18,18,&angle,DEGTORAD(90),5000));
	
	angle.x = 0.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(18,18,18,&angle,DEGTORAD(90),6000));
	
	angle.x = 1.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(25,12,12,&angle,0,8000));
	
	angle.x = 1.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(25,0,18,&angle,0,7000));
	
	angle.x = 1.0f;
	angle.y = 0.0f;
	angle.z = 0.0f;
	mAirPlane->AddKeyFrame(new KeyFrame(25,1,18,&angle,0,9000));
	//}
	//mAirPlane->AddKeyFrame(new KeyFrame(-10,-10,0,15000));
	//mAirPlane->AddKeyFrame(new KeyFrame(10,-10,0,20000));

	mModels.push_back(mAirPlane);

	mCamera = new Camera();

	D3DVIEWPORT9 view;
	d3ddev->GetViewport( &view);
	mCamera->SetAspectRatio(((float)(view.Width))/view.Height);
	
	mCamera->GetEyePos()->x = 0.0f;
	mCamera->GetEyePos()->y = 1.5f;
	mCamera->GetEyePos()->z = 75.0f;

	mCamera->SetFarPlane(100000.0f);
		
	

	Light* mLight= new Light();

	mLight->GetDirection()->x = 0.0f;
	mLight->GetDirection()->y = 0.0f;
	mLight->GetDirection()->z = 0.0f;
	


	mLight->SetOn(true);
	mLights.push_back(mLight);

	for(int i = 0; i < mLights.size(); i++ )
	{
		mLights[i]->SetLightNumber(i);
		d3ddev->SetLight(mLights[i]->GetLightNumber()
						, mLight->GetD3DLight()); 
	}		
}

void Game::LoadContent(LPDIRECT3DDEVICE9 d3ddev){
	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->LoadContent(d3ddev);
	}
}

//Draws the list of models
void Game::Draw(LPDIRECT3DDEVICE9 d3ddev){
	
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(75, 200, 100), 1.0f, 0); // clear frame
	d3ddev->BeginScene();

	//Set Camera
	d3ddev->SetTransform(D3DTS_VIEW, mCamera->GetViewMatrix());    // set the view transform to matView

    // set the projection transform
    d3ddev->SetTransform(D3DTS_PROJECTION, mCamera->GetProjectionMatrix());    // set the projection
	
	//mMesh->Draw(d3ddev);
	//Draw Models
	for(int i = 0; i < mModels.size(); i++ )
	{
		d3ddev->SetTransform(D3DTS_WORLD, &mIdentity);
		mModels[i]->Draw(d3ddev);
	}
	
	d3ddev->EndScene();
}

void Game::Release(){
	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->Release();
	}

}
